package com.yeshine.opendata.controller.admin;

import com.yeshine.opendata.entity.SysDictionary;
import com.yeshine.opendata.service.IDictionaryService;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by taohj on 2014/8/13.
 */
@Controller
@RequestMapping("/admin/dictionary")
public class AdminDictionaryController extends AbstractAdminControllerBase {

    @Autowired
    private IDictionaryService service;

    @RequestMapping("createV")
    public String createV(){

        return "admin/dictionary/create";
    }



    @ResponseBody
    @RequestMapping("create")
    public String create(
            @RequestParam(value = "type",required = false) String type,
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "value",required = false) String value,
            @RequestParam(value = "defaultValue",required = false) Integer defaultValue,
            @RequestParam(value = "description",required = false) String description
    ){
        SysDictionary sysDictionary =new SysDictionary();
        sysDictionary.setType(type);
        sysDictionary.setName(name);
        sysDictionary.setValue(value);
        sysDictionary.setDescription(description);
        sysDictionary.setDefaultValue(defaultValue);
        Result<SysDictionary> result=service.create(sysDictionary);
        result.setMessage(result.isSuccess()?"新增成功":result.getMessage());
        return result.toString();
    }


    @RequestMapping("modifyV")
    public String modifyV(
            @RequestParam("id") Integer id
    ){
        setRequestAttr("dictionary",service.getById(id).getTarget());
        return "admin/dictionary/modify";
    }


    @ResponseBody
    @RequestMapping("modify")
    public String create(
            @RequestParam(value = "id",required = false) Integer id,
            @RequestParam(value = "type",required = false) String type,
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "value",required = false) String value,
            @RequestParam(value = "defaultValue",required = false) Integer defaultValue,
            @RequestParam(value = "description",required = false) String description
    ){
        SysDictionary sysDictionary =service.getById(id).getTarget();
        sysDictionary.setType(type);
        sysDictionary.setName(name);
        sysDictionary.setValue(value);
        sysDictionary.setDescription(description);
        sysDictionary.setDefaultValue(defaultValue);
        Result<SysDictionary> result=service.modify(sysDictionary);
        result.setMessage(result.isSuccess()?"更新成功":result.getMessage());
        return result.toString();
    }


    @ResponseBody
    @RequestMapping("remove")
    public  String remove(
            @RequestParam("id") Integer id
    ){
        Result<SysDictionary> result=service.remove(new SysDictionary(id));
        result.setMessage(result.isSuccess()?"删除成功":result.getMessage());
        return  result.toString();
    }

    @RequestMapping("listV")
    public  String listV(){
        return "admin/dictionary/list";
    }

    @ResponseBody
    @RequestMapping("list")
    public  String list(
            @RequestParam(value = "page",required = false) Integer page,
            @RequestParam(value = "rows",required = false) Integer size,
            @RequestParam(value = "id",required = false)Integer id,
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "type",required = false) String type
    ){
        PageResult<SysDictionary> pageResult=new PageResult<SysDictionary>(page,size);
        SysDictionary sysDictionary =new SysDictionary();
        if (id!=null){
            sysDictionary.setId(id);
        }
        if (StringUtils.hasLength(name)){
            sysDictionary.setName(name);
        }
        if (StringUtils.hasLength(type)) {
            sysDictionary.setType(type);
        }
        pageResult.setT(sysDictionary);
        Result<SysDictionary> result=service.getByPage(pageResult);


        return result.getPageResult().toString();
    }




}
